/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_DECODER) || \
    defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include "umc_avs_fusion_core.h"
#include "umc_avs_pic.h"
#include "umc_avs_slice.h"
#include "umc_avs_memory_piece.h"
#include "umc_avs_dec_additional_info.h"

#include "umc_automatic_mutex.h"

namespace UMC
{

enum
{
    NUMBER_OF_COEFF_ROWS        = 5
};

AVSListElement<AVSFrame> *AVSFusionCore::GetFreeFrame(void)
{
    AVSListElement<AVSFrame> *pToReturn = NULL;

    pToReturn = m_FreeFrames.GetHead();

    // there are more frames in the queue, return the oldest
    if (pToReturn)
        return pToReturn;

    // there is an unsufficient amount of frames,
    // so allocate one more
    if (m_numFrames < m_maxFrames)
    {
        pToReturn = AllocateFrame();
        if (NULL == pToReturn)
            return NULL;

        pToReturn->m_frameNum = m_curFrameNum++;

        // we do not need to initialize the frame,
        // because we don't have a picture header with parameters

        m_numFrames += 1;

        // add the frame to the free list
        m_FreeFrames.AddToHead(*pToReturn);

        return pToReturn;
    }

    // run through the frame list and try to get old frame
    {
        AutomaticMutex guard(m_guard.ExtractHandle());
        AVSListElement<AVSFrame> *pHead, *pNext, *pNext2;

        pToReturn = NULL;

        // analyze 3 frames to decide,
        // which one is the oldest frame
        pHead = m_Frames.GetHead();
        pNext = pHead->GetNext();
        pNext2 = pNext->GetNext();

        if (false == pNext2->IsReference())
        {
            // we can use the frame only
            // if it was shown
            if (pNext2->IsShown())
            {
                pNext->SetNext(pNext2->GetNext());
                pToReturn = pNext2;
                
                pToReturn->m_frameNum = m_curFrameNum++;
            }
        }
        else
        {
            // it is quite a complex case

            // we can use the frame only
            // if it was shown,
            // and the following reference frames are ready
            if ((pHead->IsShown()) &&
                (pNext->IsReady()) &&
                (pNext2->IsReady()))
            {
                pToReturn = m_Frames.ExtractHead();

                pToReturn->m_frameNum = m_curFrameNum++;
            }
        }

        // remove all useless stuff from the frame
        if (pToReturn)
        {
            pToReturn->SetNext(NULL);
            PurgeFrame(pToReturn);
        }
        else
            return NULL;

        // add the frame to the free list
        m_FreeFrames.AddToHead(*pToReturn);
    }

    return pToReturn;

} // AVSListElement<AVSFrame> *AVSFusionCore::GetFreeFrame(void)

AVSListElement<AVSFrame> *AVSFusionCore::AllocateFrame(void)
{
    return new AVSListElement<AVSFrame> ();

} // AVSListElement<AVSFrame> *AVSFusionCore::AllocateFrame(void)

AVSListElement<AVSSlice> *AVSFusionCore::GetFreeSlice(bool bLargeCoeffsBuffer)
{
    AVSListElement<AVSSlice> *pTemp, *pPrev;
    size_t iRequiredBuffer;
    Ipp32s iRequiredRows;

    // calculate the size of the buffer for coefficients
    iRequiredRows = 1;
    switch (m_seqHeader.chroma_format)
    {
    case AVS_CHROMA_422_FORMAT:
        iRequiredBuffer = 64 * 8;
        break;

    default:
        iRequiredBuffer = 64 * 6;
        break;
    }
    if ((1 < m_numThreads) && (bLargeCoeffsBuffer))
    {
        Ipp32s widthMB = (m_seqHeader.horizontal_size + 15) / 16;

        iRequiredRows = NUMBER_OF_COEFF_ROWS;
        iRequiredBuffer = iRequiredBuffer * widthMB + sizeof(AVS_DEC_ADDITIONAL_INFO);
    }

    // try to find an existing piece, which fits requirements
    pPrev = NULL;
    pTemp = m_FreeSlices.GetHead();
    while (pTemp)
    {
        if (pTemp->GetCoeffsBufferSize() >= iRequiredBuffer)
            break;

        // touch the next element
        pPrev = pTemp;
        pTemp = pTemp->GetNext();
    }

    // we found a suitable element, exclude it from the list
    if (pTemp)
    {
        // it is from somewhere in the middle of the list,
        // exclude it, relink the list
        if (pPrev)
        {
            pPrev->SetNext(pTemp->GetNext());
            pTemp->SetNext(NULL);
        }
        // it was the single element in the list
        else
            m_FreeSlices.ExtractHead();
    }
    // the list is empty or all elements are too small
    else
    {
        // we need to allocate one more pieces
        if (NULL == pPrev)
        {
            pTemp = AllocateSlice();
        }
        // take the first element to re-initialize
        else
        {
            pTemp = m_FreeSlices.ExtractHead();
        }

        // initialize the memory piece
        if (UMC_OK != pTemp->InitCoeffsBuffer(iRequiredBuffer, iRequiredRows))
        {
            m_FreeSlices.AddToHead(*pTemp);
            return NULL;
        }
    }

    // terminate the chain
    if (pTemp)
    {
        pTemp->SetNext((AVSListElement<AVSSlice> *) 0);
    }

    return pTemp;

} // AVSListElement<AVSSlice> *AVSFusionCore::GetFreeSlice(bool bLargeCoeffsBuffer)

AVSListElement<AVSSlice> *AVSFusionCore::AllocateSlice(void)
{
    return new AVSListElement<AVSSlice> ();

} // AVSListElement<AVSSlice> *AVSFusionCore::AllocateSlice(void)

AVSListElement<AVSMemory> *AVSFusionCore::GetFreeMemoryPiece(size_t nRequiredSize)
{
    AVSListElement<AVSMemory> *pTemp, *pPrev;

    // try to find an existing piece, which fits requirements
    pPrev = NULL;
    pTemp = m_FreeMemory.GetHead();
    while (pTemp)
    {
        if (pTemp->GetSize() >= nRequiredSize)
            break;

        // touch the next element
        pPrev = pTemp;
        pTemp = pTemp->GetNext();
    }

    // we find a fitting element, exclude it from the list
    if (pTemp)
    {
        // it is from somewhere in the middle of the list,
        // exclude it, relink the list
        if (pPrev)
        {
            pPrev->SetNext(pTemp->GetNext());
            pTemp->SetNext(NULL);
        }
        // it was the single element in the list
        else
            m_FreeMemory.ExtractHead();
    }
    // the list is empty or all elements are too small
    else
    {
        // we need to allocate one more pieces
        if (NULL == pPrev)
        {
            pTemp = new AVSListElement<AVSMemory> ();
        }
        // take the first element to re-initialize
        else
        {
            pTemp = m_FreeMemory.ExtractHead();
        }

        // initialize the memory piece
        if (UMC_OK != pTemp->Init(nRequiredSize))
        {
            m_FreeMemory.AddToHead(*pTemp);
            return NULL;
        }
    }

    // terminate the chain
    if (pTemp)
    {
        pTemp->SetNext((AVSListElement<AVSMemory> *) 0);
    }

    return pTemp;

} // AVSListElement<AVSMemory> *AVSFusionCore::AllocateMemoryPiece(size_t nRequiredSize)

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_DECODER) ||
